﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class MenuBLL
  {
    public List<Menu_> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Menu_s.OrderBy(p => p.Sort).ToList();
      }
    }
    public List<Menu_> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Menu_s.Where(p => p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }
    public List<Menu_> GetByPosition(int position)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Menu_s.Where(p => p.Position == position).OrderBy(p => p.Sort).ToList();
      }
    }
    public List<Menu_> GetByPosition(int position, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Menu_s.Where(p => p.Position == position && p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }
    public Menu_ GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Menu_s.Where(p => p.Id == id).FirstOrDefault();
      }
    }
    public Menu_ GetById(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Menu_s.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }
    public byte GetMaxSort(byte position, int parent)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Menu_ obj = data.Menu_s.Where(p => p.Position == position && p.Parent == parent).OrderByDescending(p => p.Sort).FirstOrDefault();

        if (obj == null) return 1;
        else return (byte)(obj.Sort + 1);
      }
    }
    public List<Menu_> GetByLevel(int level)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Menu_s.Where(p => p.Level == level).ToList();
        }
    }
    public List<Menu_> GetByLevel(int level,int level2)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Menu_s.Where(p => p.Level == level&&p.Level==level2).ToList();
      }
    }
    public List<Menu_> GetByParent(int parent)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Menu_s.Where(p => p.Parent == parent).ToList();
      }
    }
    public List<Menu_> GetByParent(int parent,bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Menu_s.Where(p => p.Parent == parent && p.isEnable == isEnable).ToList();
      }
    }
    public void SwapSort(int idFirst, int idSecond)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Menu_ objFirst = data.Menu_s.Where(p => p.Id == idFirst).FirstOrDefault();
        Menu_ objSecond = data.Menu_s.Where(p => p.Id == idSecond).FirstOrDefault();

        byte tg = objFirst.Sort;
        objFirst.Sort = objSecond.Sort;
        objSecond.Sort = tg;

        data.SubmitChanges();
      }
    }
    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Menu_ obj = data.Menu_s.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;

        data.SubmitChanges();
      }
    }
    public void Add(Menu_ obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Menu_s.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }
    public void Update(Menu_ obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Menu_ menu = data.Menu_s.Where(p => p.Id == obj.Id).FirstOrDefault();
        menu.isEnable = obj.isEnable;
        menu.Link = obj.Link;
        menu.Mode = obj.Mode;
        menu.Name = obj.Name;
        menu.Name_En = obj.Name_En;
        menu.Position = obj.Position;
        menu.Sort = obj.Sort;
        menu.Target = obj.Target;
        menu.Level = obj.Level;
        if (obj.Parent != null) menu.Parent = obj.Parent;

        data.SubmitChanges();
      }
    }
    public void DeleteByParent(int parent)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Menu_> list = data.Menu_s.Where(p => p.Parent == parent).ToList();
        if (list.Count > 0)
        {
          data.Menu_s.DeleteAllOnSubmit(list);
          data.SubmitChanges();
        }
      }
    }
    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Menu_ obj = data.Menu_s.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
        {
          DeleteByParent(id);
          data.Menu_s.DeleteOnSubmit(obj);
          data.SubmitChanges();
        }        
      }
    }
  }
}
